﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using System.IO;
using StateGrid.User;

public partial class View : System.Web.UI.Page
{
    //全文文件名
    private String fileName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        if (!this.IsPostBack)
        {
            this.EditPDF();
        }
    }

    private void EditPDF()
    {
        this.fileName = (String)Request["fileName"];
        if (null == this.fileName || "".Equals(this.fileName))
        {
            String message = "错误：请不要通过非法方式查看全文";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }

        String fpath = OperationFile.FormatPath(fileName)+fileName;

        string path = fpath.Replace("\\", "/");

        MemoryStream ms = GetStream(path);
        if (null == ms)
        {
            return;
        }

        ViewFile(ms);
    }


    /// <summary>
    /// 获取内存文件流
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <returns></returns>
    private MemoryStream GetStream(String path)
    {
        MemoryStream ms = new MemoryStream(); ;
        Boolean flag = true;
        if (this.fileName.EndsWith(".PDF", true, null))
        {
            try
            {
                PdfReader reader = new PdfReader(path);  
                PdfStamper stamper = new PdfStamper(reader, ms);
                stamper.Writer.ViewerPreferences = PdfWriter.HideWindowUI;
                stamper.Writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, null, PdfWriter.AllowPrinting | PdfWriter.AllowFillIn | PdfWriter.AllowCopy);
                stamper.Writer.CloseStream = false;
                stamper.Close();
            }
            catch (Exception)
            {
                flag = false;
            }
        }
        else if (this.fileName.EndsWith(".DOC", true, null))
        {
            //读取DOC文件流
            try
            {
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Position = 0;
                fileStream.Read(buffer, 0, (int)fileStream.Length);
                fileStream.Close();

                ms.Write(buffer, 0, buffer.Length);
            }
            catch (Exception)
            {
                flag = false;
            }
        }

        if (!flag)
        {
            String message = "错误：你要查看的全文不存在";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return null;
        }
        return ms;
       
    }
    /// <summary>
    /// 查看PDF全文
    /// </summary>
    /// <param name="fs"></param>
    private void ViewFile(Stream fs)
    {
        byte[] buffer = new byte[fs.Length];
        fs.Position = 0;
        fs.Read(buffer, 0, (int)fs.Length);
        Response.Clear();
        //Response.AppendHeader("Content-Disposition", "attachment;FileName=ZWDB1000-2001-01-001.PDF");
        //Response.AddHeader("Content-Disposition", "attachment;FileName=ZWDB1000-2001-01-001.PDF");
        Response.AddHeader("Content-Length", fs.Length.ToString());
        Response.AddHeader("Content-Disposition", "inline;FileName="+this.fileName);
        if (this.fileName.EndsWith(".PDF", true, null))
        {
            Response.ContentType = "application/pdf";
        }
        else if (this.fileName.EndsWith(".DOC", true, null))
        {
            Response.ContentType = "application/msword";
        }
        fs.Close();
        Response.BinaryWrite(buffer);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        Response.End();
    }
}